package com.wzy.lock;

import java.util.concurrent.CountDownLatch;

/**
 * 描述  :
 * 创建人:    Wzy
 * 创建时间: 2020-06-06  23:46
 */
public class Test {
    private static int num = 0;
    private static CountDownLatch countDownLatch = new CountDownLatch(10);
    private static ZKLock zkLock = ZKLock.getInstance();

    public static void sum() {
        zkLock.lock(1);
        num++;
        // System.out.println(num);
        zkLock.unlock(1);
    }

    /**
     * 测试确认可以使用,结果准确
     *
     * @param args
     */
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                for (int j = 0; j < 100; j++) {
                    sum();
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
            countDownLatch.countDown();
        }

        try {
            countDownLatch.await();
            System.out.println(num);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }


}
